<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>数据监控 | 高校健康云</title>
        <header th:replace="header::html"></header>
        <style>
            .charts .layui-card {
                min-height: 510px;
            }
            .layui-card-header {
                display: flex;
                flex-direction: column;
                justify-content: space-between;
                min-height: 42px;
                height: unset;
            }
            .layui-card-header span {
                font-size: 18px;
                font-weight: bold;
            }
        </style>
    </head>
    <body>
        <div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="/">首页</a>
            <a>
                <cite>数据监控</cite></a>
          </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
                <i class="layui-icon" style="line-height:30px">ဂ</i></a>
        </div>

        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-sm12 layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-body">
                            <form class="layui-form  layui-col-space5">
                                <div class="layui-inline layui-show-xs-block">
                                    <button class="layui-btn"  lay-submit="" lay-filter="refresh"><i class="layui-icon">ဂ</i></button>
                                </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <div class="layui-input-inline">
                                        <input type="text" name="startDate" class="layui-input" id="startDate" placeholder="yyyy-MM-dd">
                                    </div>
                                </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <button class="layui-btn"  lay-submit="" lay-filter="search"><i class="layui-icon">&#xe615;</i></button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
            <div class="charts layui-row layui-col-space15">
                <div class="layui-col-sm12 layui-col-md6">
                    <div class="layui-card">
                        <div class="layui-card-header">
                            <div><span>近7天体温趋势</span></div>
                            <div>
                                <blockquote th:if="${temperatureLine gt 0}" class="layui-elem-quote">
                                    7天内高热人数增长比例超过20%，需采取紧急切断传染途径措施
                                </blockquote>
                            </div>
                        </div>
                        <div class="layui-card-body" style="min-height: 380px;">
                            <div id="temperature-line" class="layui-col-sm12" style="height: 380px;"></div>
                        </div>
                    </div>
                </div>
                <div class="layui-col-sm12 layui-col-md6">
                    <div class="layui-card">
                        <div class="layui-card-header">
                            <div><span>当日体温数据</span></div>
                            <div>
                                <blockquote th:if="${temperature gt 0}" class="layui-elem-quote">请通知学校疫情防控小组人员送学生去医院诊治</blockquote>
                            </div>
                        </div>
                        <div class="layui-card-body" style="min-height: 380px;">
                            <div id="main1" class="layui-col-sm12" style="height: 380px;"></div>

                        </div>
                    </div>
                </div>
                <div class="layui-col-sm12 layui-col-md6">
                    <div class="layui-card">
                        <div class="layui-card-header">
                            <div><span>当日旅行数据</span></div>
                            <div>
                                <blockquote th:if="${travel gt 0}" class="layui-elem-quote">请及时通知学生需按规定隔离14天，并出具相关健康证明</blockquote>
                            </div>
                        </div>
                        <div class="layui-card-body" style="min-height: 380px;">
                            <div id="main2" class="layui-col-sm12" style="height: 380px;"></div>

                        </div>
                    </div>
                </div>
                <div class="layui-col-sm12 layui-col-md6">
                    <div class="layui-card">
                        <div class="layui-card-header">
                            <div><span>当日症状数据</span></div>
                            <div>
                                <blockquote th:if="${physicalCondition gt 0}" class="layui-elem-quote">请及时通知学校疫情防控小组人员，做到早隔离、早治疗</blockquote>
                            </div>
                        </div>
                        <div class="layui-card-body" style="min-height: 380px;">
                            <div id="main3" class="layui-col-sm12" style="height: 380px;"></div>

                        </div>
                    </div>
                </div>
                <!--<div class="layui-col-sm12 layui-col-md6">
                    <div class="layui-card">
                        <div class="layui-card-header">近14天填报趋势</div>
                        <div class="layui-card-body" style="min-height: 380px;">
                            <div id="main4" class="layui-col-sm12" style="height: 380px;"></div>

                        </div>
                    </div>
                </div>-->
            </div>
        </div>
        </div>

<!--        <script src="https://cdn.bootcss.com/echarts/4.2.1-rc1/echarts.min.js"></script>-->
        <script type="text/javascript" th:src="@{/echarts/echarts.min.js}"></script>
        <script type="text/javascript">
            var today =  new Date();
            var startDate = dateUtils.format(new Date(), "yyyy-MM-dd");
            $(document).on("click", function() {
                // console.log("document click")
                var userId = localStorage.getItem("userId");
                if (!userId) {
                    // console.log("need login")
                    location.href = "/"
                }
            })
            layui.use(['laydate', 'form'], function() {
                var form = layui.form;
                var laydate = layui.laydate;

                //常规用法
                laydate.render({
                    elem: '#startDate'
                    ,value: today
                    ,isInitValue: true
                    ,max: dateUtils.format(today, "yyyy-MM-dd")
                });
                //搜索
                form.on('submit(search)', function(data){
                    var startDate = data.field.startDate;
                    refresh(startDate)
                    return false;
                })
                form.on('submit(refresh)', function(data){
                    console.log("submit(refresh) ////////// ", data )
                    var startDate = data.field.startDate;
                    $.ajax({
                        url:"/report/chart/refresh",
                        data: { },
                        dataType:'json'
                    }).done(function (res){
                        console.log("submit(refresh) //////////  res ", res )
                        refresh(startDate)
                    })
                    return false;
                })

            })

            function refresh(startDate){
                console.log("startDate ////////// ", startDate )
                updateTemperatureChartData(startDate);
                updateTravelChartData(startDate);
                updatePhysicalConditionChartData(startDate);
                updateTemperatureLineData(startDate);
            }
            /*$(document).ready( function (){
                console.log("$(document).ready")
                var dictSet =  dictUtils.getByTypes("physical_condition,travel,temperature")
                console.log("dictSet ... ", dictSet)
            })*/

            var lineChart = echarts.init(document.getElementById('temperature-line'));
            var option = {
                // title: {
                //     text: '近7天体温趋势'
                // },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    // data: ['邮件营销', '联盟广告', '视频广告', '直接访问', '搜索引擎']
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    // data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
                },
                yAxis: {
                    type: 'value'
                },
                series: []
            };
            lineChart.setOption(option);
            function updateTemperatureLineData(time){
                $.ajax({
                    url:"/report/chart/temperatureLine",
                    // async:false,
                    data: {
                        endDate: time || startDate
                    },
                    dataType:'json'
                }).done(function (res){
                    console.log("/report/chart/temperatureLine ...... ", res)
                    var keySet = {
                        NORMAL: "正常 37.3℃ 以下",
                        LOW: "低热 37.3-37.9℃",
                        MIDDLE: "中热 38-39℃",
                        HIGH: "高热 39℃以上",
                    }
                    let styleMap = {
                        'NORMAL': '#67C23A',
                        'LOW': '#009688',
                        'MIDDLE': '#E6A23C',
                        'HIGH': '#F56C6C'
                    };
                    var legend = [];
                    var xAxis = null;
                    var data = [];
                    for (var index in res.data) {
                        var item =  res.data[index];
                        var values = item.map(function (item) {
                            return item.value;
                        })
                        if (!xAxis) {
                            xAxis = item.map(function (item) {
                                return item.reportDate;
                            })
                        }
                        var title = keySet[index];
                        legend.push(title);
                        data.push({
                            name: title,
                            type: 'line',
                            stack: '总量',
                            data: values,
                            itemStyle : {
                                normal : {
                                    color: styleMap[index],
                                    lineStyle:{
                                        color: styleMap[index]
                                    }
                                }
                            },
                        })
                    }
                    var option = {
                        legend: {
                            data:legend
                        },
                        xAxis: {
                            data: xAxis
                        },
                        series: data
                    };
                    console.log("option .... ", option)
                    lineChart.setOption(option);

                })
            }
            updateTemperatureLineData(startDate);

            // 基于准备好的dom，初始化echarts实例
            var myChart1 = echarts.init(document.getElementById('main1'));
            var option = {
                // title: {
                //     text: '当日体温数据',
                //     // subtext: subTitle,
                //     left: 'center'
                // },
                tooltip: {
                    trigger: 'item',
                    formatter: '{a} <br/>{b} : {c} ({d}%)'
                },
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    data: []
                },
                series: [
                    {
                        name: '当日体温数据',
                        type: 'pie',
                        radius: '55%',
                        center: ['50%', '60%'],
                        data: [],
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
            myChart1.setOption(option);
            function updateTemperatureChartData(time){
                $.ajax({
                    url:"/report/chart/temperature",
                    // async:false,
                    data: {
                        startDate: time || startDate
                    },
                    dataType:'json'
                }).done(function (res){
                    console.log("/report/chart/temperature ...... ", res)
                    var arr = [];
                    var keySet = {
                        NORMAL: "正常 37.3℃ 以下",
                        LOW: "低热 37.3-37.9℃",
                        MIDDLE: "中热 38-39℃",
                        HIGH: "高热 39℃以上",
                    }
                    if (res.data && res.data.length > 0) {
                        arr = res.data.map(function (item) {
                            let {
                                name,value
                            } = item;
                            // let col = dictUtils.matchKey(dictSet, name, "temperature");
                            let styleMap = {
                                'NORMAL': '#67C23A',
                                'LOW': '#009688',
                                'MIDDLE': '#E6A23C',
                                'HIGH': '#F56C6C'
                            };
                            return {
                                value,
                                "name": keySet[name], //col
                                itemStyle: {
                                    color: styleMap[name]
                                }
                            }
                        })
                    }
                    let subTitle = arr&&arr.length>0 ? "" : "暂时还没有数据";

                    let names = res.data.map(function (item) { return keySet[item.name]; });
                    var option = {
                        title: {
                            subtext: subTitle,
                        },
                        legend: {
                            data: names
                        },
                        series: [
                            {
                                name: '当日体温数据',
                                data: arr,
                            }
                        ]
                    };
                    // 使用刚指定的配置项和数据显示图表。
                    myChart1.setOption(option);
                })
            }
            updateTemperatureChartData(startDate);

            // 基于准备好的dom，初始化echarts实例
            var myChart2 = echarts.init(document.getElementById('main2'));
            var option = {
                // title: {
                    // text: '当日出行数据',
                    // subtext: subTitle,
                    // left: 'center'
                // },
                tooltip: {
                    trigger: 'item',
                    formatter: '{a} <br/>{b} : {c} ({d}%)'
                },
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    data: []
                },
                series: [
                    {
                        name: '访问来源',
                        type: 'pie',
                        radius: '55%',
                        center: ['50%', '60%'],
                        data: [],
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
            myChart2.setOption(option);
            function updateTravelChartData(time) {
                $.ajax({
                    url:"/report/chart/travel",
                    // async:false,
                    data: {
                        startDate: time || startDate
                    },
                    dataType:'json'
                }).done(function (res){
                    console.log("/report/chart/travel ...... ", res)
                    var arr = [];
                    let keySet = {
                        "0":"近期没有出行",
                        "1":"去过低风险地区",
                        "2":"去过中风险地区",
                        "3":"去过高风险地区",
                    }
                    let styleMap = {
                        '0': '#67C23A',
                        '1': '#009688',
                        '2': '#E6A23C',
                        '3': '#F56C6C'
                    };
                    if (res.data && res.data.length > 0) {
                        arr = res.data.map(function (item) {
                            let { name,value } = item;
                            // let col = dictUtils.matchKey(dictSet, name, "temperature");

                            return {
                                value,
                                "name": keySet[name], //col
                                itemStyle: {
                                    color: styleMap[name]
                                }
                            }
                        })
                    }
                    let names = res.data.map(function (item) { return keySet[item.name]; });
                    let subTitle = arr&&arr.length>0 ? "" : "暂时还没有数据";
                    var option = {
                        // title: {
                        //     subtext: subTitle,
                        // },
                        legend: {
                            data: names
                        },
                        series: [{
                                name: '访问来源',
                                data: arr,
                            }]
                    };
                    // 使用刚指定的配置项和数据显示图表。
                    myChart2.setOption(option);
                })
            }
            updateTravelChartData();

            // 指定图表的配置项和数据

            // 基于准备好的dom，初始化echarts实例
            var myChart3 = echarts.init(document.getElementById('main3'));
            var option = {
                // title: {
                //     text: '当日症状数据',
                //     subtext: "",
                //     left: 'center'
                // },
                color: ['#3398DB'],
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                        type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: [
                    {
                        type: 'category',
                        data: [],
                        axisTick: {
                            alignWithLabel: true
                        },
                        axisLabel:{
                            interval:0,//横轴信息全部显示
                            rotate:45,//度角倾斜显示
                            formatter:function(value){//只显示五个字 其余省略号
                                return value.length > 5?value.substring(0,5)+'...':value;
                            }
                        }
                    }
                ],
                yAxis: [{
                        type: 'value'
                    }],
                series: [{
                        // name: '直接访问',
                        type: 'bar',
                        // barWidth: '60%',
                        data: []
                    }]
            };
            myChart3.setOption(option);
            function updatePhysicalConditionChartData(time) {
                $.ajax({
                    url:"/report/chart/physicalCondition",
                    // async:false,
                    data: {
                        startDate: time||startDate
                    },
                    dataType:'json'
                }).done(function (res){
                    console.log("/report/chart/physicalCondition ...... ", res)
                    var arr = [];
                    if (res.data && res.data.length > 0) {
                        arr = res.data.map(function (item) { return item.value; });
                    }
                    let keySet = {
                        'travel': '14天内曾居住或前往疫情高发地',
                        'contact': '两周内有与确诊患者接触',
                        'normal': '没有出现症状',
                        'rheum': '感冒样症状：乏力、精神差、咳嗽、发烧、肌肉痛、头痛',
                        'polypnea': '喘憋、呼吸急促',
                        'vomiting': '恶心呕吐、腹泻',
                        'flustered': '心慌、胸闷',
                        'conjunctivitis': '结膜炎（红眼病样表现：眼睛涩、红、分泌物）',
                }
                    let names = res.data.map(function (item) { return keySet[item.name]; });
                    // let color = res.data.map(function (item) { return item.name == "normal" ? "#67C23A":"#3398DB"; });
                    let subTitle = arr&&arr.length>0 ? "" : "暂时还没有数据";
                    // 指定图表的配置项和数据
                    var option = {
                        // color:color,
                        xAxis: [
                            {
                                type: 'category',
                                data: names,
                            }
                        ],
                        series: [
                            {
                                // name: '直接访问',
                                type: 'bar',
                                // barWidth: '60%',
                                data: arr
                            }
                        ]
                    };
                    // 使用刚指定的配置项和数据显示图表。
                    myChart3.setOption(option);
                })
            }
            updatePhysicalConditionChartData();



            // 基于准备好的dom，初始化echarts实例
            /*var myChart4 = echarts.init(document.getElementById('main4'));
            var option = {
                color: ['#3398DB'],
                title: {
                    text: '近14天填报趋势',
                    // subtext: subTitle,
                    left: 'center'
                },
                tooltip: {
                    trigger: 'axis',
                    // formatter: '{a} <br/>{b} : {c} ({d}%)'
                    axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                        type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                    }
                },
                xAxis: {
                    type: 'category',
                    data: ''
                },
                yAxis: {
                    type: 'value'
                },
                series: [{
                    data: '',
                    type: 'line'
                }]
            };
            myChart4.setOption(option);
            function updateCountChartData(time) {
                $.ajax({
                    url:"/report/chart/count",
                    // async:false,
                    data: {
                        endDate: time || startDate
                    },
                    dataType:'json'
                }).done(function (res){
                    console.log("/report/chart/count ...... ", res)
                    var arr = [];
                    if (res.data && res.data.length > 0) {
                        arr = res.data.map(function (item) { return item.value; });
                    }
                    let names = res.data.map(function (item) { return item.name; });
                    let subTitle = arr&&arr.length>0 ? "" : "暂时还没有数据";
                    // 指定图表的配置项和数据
                    var option = {
                        // color: color,
                        title: {
                            text: '近14天填报趋势',
                            subtext: subTitle,
                            left: 'center'
                        },
                        xAxis: {
                            type: 'category',
                            data: names
                        },
                        yAxis: {
                            type: 'value'
                        },
                        series: [{
                            data: arr,
                            type: 'line'
                        }]
                    };
                    // 使用刚指定的配置项和数据显示图表。
                    myChart4.setOption(option);
                })
            }
            updateCountChartData();*/

        </script>
    </body>

</html>